<?php


namespace app\base\service;

use think\admin\service\AdminService;

use think\Cache;

/**
 * 榜眼数据服务
 * Class AdminService
 * @package think\admin\service
 */
class TophubdataService extends AdminService
{

    protected $authorization = '';

    protected $url = 'https://api.tophubdata.com';

    protected function getAuthorization($user_id)
    {
        $api_config_type = $this->app->db->name("ai_user")->where(['id'=>$user_id])->value("api_config_type");
        if($api_config_type==1){
            $user_id=10000;
        }
        $this->authorization = $this->app->db->name('ai_api_config')
            ->where(['user_id' => $user_id,'type'=>1])->value('access_key');
        if(empty($this->authorization)){
            return $this->resulet(0,'请先配置authorization');
        }
    }

    protected function resulet($code=0,$msg='',$data=[])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

    public function search($user_id,$q='',$p=1,$hashid="")
    {
        $this->getAuthorization($user_id);
        $url = $this->url . '/search';
        $data = [
            'q' => $q,
            'p' => $p
        ];
        if($hashid){
            $data['hashid'] = $hashid;
        }
        $options['headers'] = ['Authorization:'.$this->authorization];
        $res = http_get($url,$data,$options);
        $res = json_decode($res,true);
        if($res['error']){
            return $this->resulet(0,$res['msg']);
        }else{
            return $this->resulet(1,'',$res['data']);
        }


    }

}
